﻿using AdornerPopup.Core.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace AdornerPopup.Sample
{
    public class SampleDataViewModelModel : ViewModelBase
    {
        public ICommand SearchCommand { get; set; }
        public ObservableCollection<string> Collection { get; set; }
        public ObservableCollection<string> SearchResults { get; set; }
        public bool IsOpen { get; set; }

        public SampleDataViewModelModel()
        {
            Collection = new ObservableCollection<string>();
            SearchResults = new ObservableCollection<string>();
            SearchCommand = new RelayCommand<string>(SearchHandler);

            Collection.Add("Jins");
            Collection.Add("Hello");
            Collection.Add("World");
            Collection.Add("EveryOne");
            Collection.Add("Data1");
            Collection.Add("Data2");
            Collection.Add("Data3");
            Collection.Add("Data4");
            Collection.Add("Data5");
            Collection.Add("Data6");

            Search("");
        }

        private void SearchHandler(string obj)
        {
            Search(obj);

            IsOpen = true;
            RaisePropertyChanged(nameof(IsOpen));
        }

        private void Search(string obj)
        {
            this.SearchResults.Clear();

            foreach (var item in this.Collection.Where(p => string.IsNullOrWhiteSpace(obj) || p.Contains(obj)))
            {
                this.SearchResults.Add(item);
            }
        }
    }
}
